<template>
  <div id="RoomInfo" class="page-wrapper">
    <h1 class="main-title">基础设置</h1>
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card"></div>
      <el-button type="primary" @click="handleSubmitClick">添加</el-button>
    </div>
    <div class="wrapper">
      <PanelGroup :roomInfo="roomInfo" :adminName="adminName" />
    </div>
    <div class="wrapper main-card">
      <!-- Listen for the 'refresh-data' event emitted by the child component -->
      <carousel :table-data="carouselinfo" @refresh-data="fetchData" />
    </div>
    <!-- 商品详情弹窗 -->
    <el-dialog
      title="商品详情"
      :visible.sync="dialogVisible"
      width="800px"
      height="800px"
    >
      <el-form class="form" ref="form" :model="modifydata">
        <div class="form-item-wrapper">
          <!-- 上传图片 -->
          <el-form-item label="上传图片">
            <el-upload
              class="upload-demo"
              ref="imageUpload"
              :before-upload="beforeUpload"
              :on-change="handleImageChange"
              list-type="picture-card"
              :show-file-list="false"
              accept="image/*"
              :auto-upload="false"
            >
              <i class="el-icon-plus"></i>
              <img v-if="imageUrl" :src="imageUrl" class="preview-image" />
            </el-upload>
          </el-form-item>
          <!-- 上传视频 -->
          <el-form-item label="上传视频">
            <el-upload
              class="upload-demo"
              ref="videoUpload"
              :before-upload="beforeVideoUpload"
              :on-change="handleVideoChange"
              list-type="text"
              :show-file-list="false"
              accept="video/*"
              :auto-upload="false"
            >
              <i class="el-icon-video-camera"></i>
              <div class="el-upload__tip">上传视频</div>
              <video v-if="videoUrl" controls class="preview-video">
                <source :src="videoUrl" type="video/mp4" />
                您的浏览器不支持 video 标签。
              </video>
            </el-upload>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import carousel from './components/UserTable'
import { getcarousel, uploadcarousel } from '@/api/pcclinet'

export default {
  name: 'RoomInfo',
  components: {
    carousel
  },
  data() {
    return {
      dialogVisible: false,
      carouselinfo: {}, // 存储轮播信息
      dialogdata: {}, // 弹窗数据
      modifydata: {}, // 表单数据
      imageFile: null, // 存储图片文件
      videoFile: null, // 存储视频文件
      imageUrl: '', // 图片预览 URL
      videoUrl: '' // 视频预览 URL
    }
  },
  mounted() {
    this.fetchData() // 组件挂载时获取数据
  },
  methods: {
    // 获取服务器数据
    fetchData() {
      getcarousel().then(res => {
        this.carouselinfo = res.data // 将服务器返回的数据存储到carouselinfo
        //  console.log(this.carouselinfo)
      })
    },
    handleSubmitClick() {
      this.dialogVisible = true // 打开弹窗
    },
    // 上传图片前的校验
    beforeUpload(file) {
      const isImage = file.type.startsWith('image/') // 判断文件类型
      const isLt2M = file.size / 1024 / 1024 < 0.06; // 判断文件大小
      if (!isImage) {
        this.$message.error('只能上传图片文件！') // 非图片文件提示错误
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 60k!') // 图片文件过大提示错误
      }
      return isImage && isLt2M // 返回校验结果
    },
    // 处理图片变化
    handleImageChange(file) {
      this.imageFile = file.raw // 存储图片文件
      this.imageUrl = URL.createObjectURL(this.imageFile) // 生成预览 URL
    },
    // 上传视频前的校验
    beforeVideoUpload(file) {
      const isVideo = file.type.startsWith('video/') // 判断文件类型
      const isLt5M = file.size / 1024 / 1024 < 5; // 判断文件大小是否小于5MB
      if (!isVideo) {
        this.$message.error('只能上传视频文件！') // 非视频文件提示错误
      }
      if (!isLt5M) {
        this.$message.error('上传视频大小不能超过 5MB!') // 视频文件过大提示错误
      }
      return isVideo && isLt5M // 返回校验结果
    },
    // 处理视频变化
    handleVideoChange(file) {
      this.videoFile = file.raw // 存储视频文件
      this.videoUrl = URL.createObjectURL(this.videoFile) // 生成预览 URL
    },
    // 提交表单并上传文件
    handleSubmit() {
      if (!this.imageFile && !this.videoFile) {
        this.$message.error('请上传图片或视频文件！')
        return
      }

      // 调用 uploadcarousel 方法
      uploadcarousel({
        signatureImage: this.imageFile,
        mp4: this.videoFile
      })
        .then(res => {
          this.$message.success('上传成功！')
          this.dialogVisible = false // 关闭弹窗
          this.fetchData() // 重新获取数据
          // 清空文件和预览
          this.imageFile = null
          this.videoFile = null
          this.imageUrl = ''
          this.videoUrl = ''
        })
        .catch(error => {
          this.$message.error('上传失败，请重试！')
          console.error(error)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
#RoomInfo {
  .qianru {
    color: #fff;
    background-color: #1890ff;
    float: right;
  }
  > .wrapper {
    margin: 40px 0;
  }
  .selector-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center; // 垂直居中
  }
  .form-wrapper {
    margin-bottom: 40px;
    .btn-wrapper {
      display: flex;
      flex-direction: row-reverse;
    }
  }
  .search-wrapper {
    display: flex;
  }
  .el-select {
    display: block;
    position: relative;
  }
  .button-wrapper {
    display: flex;
    justify-content: flex-end; // 靠右对齐
    align-items: center; // 垂直居中
  }
}
.upload-demo {
  width: 100%;
}
.preview-image {
  width: 100px;
  height: 100px;
  object-fit: cover; // 保持图片比例
}
.preview-video {
  width: 300px;
  height: 200px;
}
</style>
